/*
    Copyright 2006 Andrew Wilkinson <andrew@indiegigs.co.uk>

    This file is part of libgp2x.

    libgp2x is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    libgp2x is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libgp2x; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdlib.h>

#include "gp2x_hardware.h"

#define INTERNAL_JOYSTICK_UP        (1<<0)
#define INTERNAL_JOYSTICK_UPLEFT    (1<<1)
#define INTERNAL_JOYSTICK_LEFT      (1<<2)
#define INTERNAL_JOYSTICK_DOWNLEFT  (1<<3)
#define INTERNAL_JOYSTICK_DOWN      (1<<4)
#define INTERNAL_JOYSTICK_DOWNRIGHT (1<<5)
#define INTERNAL_JOYSTICK_RIGHT     (1<<6)
#define INTERNAL_JOYSTICK_UPRIGHT   (1<<7)
#define INTERNAL_JOYSTICK_PUSH      (1<<11)
#define INTERNAL_START              (1<< 8)
#define INTERNAL_SELECT             (1<< 9)
#define INTERNAL_TRIGGER_LEFT       (1<<10)
#define INTERNAL_TRIGGER_RIGHT      (1<<11)
#define INTERNAL_A                  (1<<12)
#define INTERNAL_B                  (1<<13)
#define INTERNAL_X                  (1<<14)
#define INTERNAL_Y                  (1<<15)
#define INTERNAL_VOL_DOWN           (1<< 7)
#define INTERNAL_VOL_UP             (1<< 6)

void GP2x_hardware_events() {
    SDL_Event event;

    unsigned short r1 = ~(GP2x_register(GP2x_REG_BUTTONS_1));
    unsigned short r2 = ~(GP2x_register(GP2x_REG_BUTTONS_2));
    unsigned short r3 = ~(GP2x_register(GP2x_REG_JOYSTICK));

    while(SDL_PollEvent(&event)) {
        switch(event.type) {
        case SDL_KEYDOWN:
            if(event.key.keysym.sym == SDLK_w) {
                r3 |= INTERNAL_JOYSTICK_UP;
            } else if(event.key.keysym.sym == SDLK_q) {
                r3 |= INTERNAL_JOYSTICK_UPLEFT;
            } else if(event.key.keysym.sym == SDLK_a) {
                r3 |= INTERNAL_JOYSTICK_LEFT;
            } else if(event.key.keysym.sym == SDLK_z) {
                r3 |= INTERNAL_JOYSTICK_DOWNLEFT;
            } else if(event.key.keysym.sym == SDLK_x) {
                r3 |= INTERNAL_JOYSTICK_DOWN;
            } else if(event.key.keysym.sym == SDLK_c) {
                r3 |= INTERNAL_JOYSTICK_DOWNRIGHT;
            } else if(event.key.keysym.sym == SDLK_d) {
                r3 |= INTERNAL_JOYSTICK_RIGHT;
            } else if(event.key.keysym.sym == SDLK_e) {
                r3 |= INTERNAL_JOYSTICK_UPRIGHT;
            } else if(event.key.keysym.sym == SDLK_s) {
                r2 |= INTERNAL_JOYSTICK_PUSH;
            } else if(event.key.keysym.sym == SDLK_BACKSLASH) {
                r2 |= INTERNAL_VOL_DOWN;
            } else if(event.key.keysym.sym == SDLK_SLASH) {
                r2 |= INTERNAL_VOL_UP;
            } else if(event.key.keysym.sym == SDLK_i) {
                r1 |= INTERNAL_Y;
            } else if(event.key.keysym.sym == SDLK_j) {
                r1 |= INTERNAL_A;
            } else if(event.key.keysym.sym == SDLK_k) {
                r1 |= INTERNAL_B;
            } else if(event.key.keysym.sym == SDLK_m) {
                r1 |= INTERNAL_X;
            } else if(event.key.keysym.sym == SDLK_b) {
                r1 |= INTERNAL_SELECT;
            } else if(event.key.keysym.sym == SDLK_n) {
                r1 |= INTERNAL_START;
            }
            break;
        case SDL_KEYUP:
            if(event.key.keysym.sym == SDLK_w) {
                r3 = ~((~r3) | INTERNAL_JOYSTICK_UP);
            } else if(event.key.keysym.sym == SDLK_q) {
                r3 = ~((~r3) | INTERNAL_JOYSTICK_UPLEFT);
            } else if(event.key.keysym.sym == SDLK_a) {
                r3 = ~((~r3) | INTERNAL_JOYSTICK_LEFT);
            } else if(event.key.keysym.sym == SDLK_z) {
                r3 = ~((~r3) | INTERNAL_JOYSTICK_DOWNLEFT);
            } else if(event.key.keysym.sym == SDLK_x) {
                r3 = ~((~r3) | INTERNAL_JOYSTICK_DOWN);
            } else if(event.key.keysym.sym == SDLK_c) {
                r3 = ~((~r3) | INTERNAL_JOYSTICK_DOWNRIGHT);
            } else if(event.key.keysym.sym == SDLK_d) {
                r3 = ~((~r3) | INTERNAL_JOYSTICK_RIGHT);
            } else if(event.key.keysym.sym == SDLK_e) {
                r3 = ~((~r3) | INTERNAL_JOYSTICK_UPRIGHT);
            } else if(event.key.keysym.sym == SDLK_s) {
                r2 = ~((~r2) | INTERNAL_JOYSTICK_PUSH);
            } else if(event.key.keysym.sym == SDLK_BACKSLASH) {
                r2 = ~((~r2) | INTERNAL_VOL_DOWN);
            } else if(event.key.keysym.sym == SDLK_SLASH) {
                r2 = ~((~r2) | INTERNAL_VOL_UP);
            } else if(event.key.keysym.sym == SDLK_i) {
                r1 = ~((~r1) | INTERNAL_Y);
            } else if(event.key.keysym.sym == SDLK_j) {
                r1 = ~((~r1) | INTERNAL_A);
            } else if(event.key.keysym.sym == SDLK_k) {
                r1 = ~((~r1) | INTERNAL_B);
            } else if(event.key.keysym.sym == SDLK_m) {
                r1 = ~((~r1) | INTERNAL_X);
            } else if(event.key.keysym.sym == SDLK_b) {
                r1 = ~((~r1) | INTERNAL_SELECT);
            } else if(event.key.keysym.sym == SDLK_n) {
                r1 = ~((~r1) | INTERNAL_START);
            }
            break;
        case SDL_QUIT:
            GP2x_finalize();
            exit(0);
        }
    }

    GP2x_register(GP2x_REG_BUTTONS_1) = ~r1;
    GP2x_register(GP2x_REG_BUTTONS_2) = ~r2;
    GP2x_register(GP2x_REG_JOYSTICK) = ~r3;

}
